package paper.bayes;

import java.util.Hashtable;
import java.util.Enumeration;
import java.io.*;

import paper.bayes.PDF;

public class Bayes {
    private int classes;
    private PDF[] pdf;

    public Bayes(int numberClasses) {
        classes = numberClasses;
        pdf = new PDF[classes]; 
    }

    public Bayes(File f0, File f1)  {
        try {
            classes = 2;
            pdf = new PDF[classes];
            pdf[0] = new PDF(f0);
            pdf[1] = new PDF(f1);
        } catch (Exception e) {
            System.err.println("Cannot read bayesian classifier");
            System.exit(-1);
        }
    }

    public Bayes(File f0, File f1, File f2)  {
        try {
            classes = 3;
            pdf = new PDF[classes];
            pdf[0] = new PDF(f0);
            pdf[1] = new PDF(f1);
            pdf[2] = new PDF(f2);
        } catch (Exception e) {
            System.err.println("Cannot read bayesian classifier");
            System.exit(-1);
        }
    }

    public Bayes(File f0, File f1, File f2, File f3)  {
        try {
            classes = 4;
            pdf = new PDF[classes];
            pdf[0] = new PDF(f0);
            pdf[1] = new PDF(f1);
            pdf[2] = new PDF(f2);
            pdf[3] = new PDF(f3);
        } catch (Exception e) {
            System.err.println("Cannot read bayesian classifier");
            System.exit(-1);
        }
    }

    public void setPDF(int classNumber, PDF p) {
        pdf[classNumber] = p;
    }
    
    public int classify(String x) {
        return classify(x, new double[classes]);
    }
    
    public int wordCount;

    public int classify(String x, double[] prob) {
        double total = 0;
        double biggest = 0.0;
        int biggestI = -1;
        if (x.equals("and")) {
            prob[0] = 0.4;
            prob[1] = 0.4;
            if (prob.length > 3) {
                prob[2] = 0.2;
                prob[3] = 0.1;
            }
            return 0;
        }
        if (x.equals("of")) {
            prob[0] = 0.1;
            prob[1] = 0.3;
            if (prob.length > 3) {
                prob[2] = 0.3;
                prob[3] = 0.3;
            }
            return 1;
        }
        if (x.equals("in")) {
            prob[0] = 0.21;
            prob[1] = 0.2;
            if (prob.length > 3) {
                prob[2] = 0.1;
                prob[3] = 0.5;
            }
            return 3;
        }
        if (x.equals("eds")) {
            prob[0] = 0.7;
            prob[1] = 0.1;
            if (prob.length > 3) {
                prob[2] = 0.1;
                prob[3] = 0.1;
            }
            return 0;
        }
        if (x.equals("ed")) {
            prob[0] = 0.7;
            prob[1] = 0.1;
            if (prob.length > 3) {
                prob[2] = 0.1;
                prob[3] = 0.1;
            }
            return 0;
        }
        if (x.equals("proceedings")) {
            prob[0] = 0;
            prob[1] = 0.02;
            if (prob.length > 3) {
                prob[2] = 0.01;
                prob[3] = 0.97;
            }
            return 3;
        }
        for (int i = 0; i < classes; i++) {
            Double d = pdf[i].get(x);
            if (d == null) {
                prob[i] = 0;
            } else {
                prob[i] = d;
                total += prob[i];
                if (prob[i] > biggest) {
                    biggest = prob[i];
                    biggestI = i;
                }
            }
        }
        wordCount = 1;
        if (total == 0 && x.length() > 10) {
//            System.out.println("Split " + x + "?");
            return classifyAfterSplit(x, prob);
        }        
        for (int i = 0; i < classes; i++) {
            prob[i] /= total;
        }
        return biggestI;
    }

    public int classifyAfterSplit(String x, double[] prob) {
        for (int i = x.length()-1; i > 0; i--) {
            String first = x.substring(0,i);
            boolean found = false;
            for (int j = 0; j < classes; j++) {
                Double d = pdf[j].get(first);
                if (d != null) {
                    found = true;
                    break;
                }
            }
            if (found) {
                wordCount++;
                classifyAfterSplit(x.substring(i), prob);
                double biggest = 0;
                int biggestI = -1;
                for (int j = 0; j < classes; j++) {
                    Double d = pdf[j].get(first);
                    if (d != null) {
                        prob[j] *= d;
                    } else {
                        prob[j] *= 0.01;
                    }
                    if (prob[j] > biggest) {
                        biggest = prob[j];
                        biggestI = j;
                    }
                }
                return biggestI;
            }
        }
        return -1;
    }

    public static void main(String args[]) throws FileNotFoundException, IOException {
        PDF p = new PDF();
        p.build(new FileReader("authors"));
        p.save(new File("lib/authors.hash"));
        p = new PDF();
        p.build(new FileReader("titles"));
        p.save(new File("lib/titles.hash"));
    }
}
